﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RdpClient
{
	public class Form_Settings : Form
	{
		private List<RdpInfo> ListSM = new List<RdpInfo>();
		private IContainer components = null;
		private Button btnSave;
		private Button btnClose;
		private DataGridView grid;
		private Button btnDelete;
		private Button btnAdd;
		private DataGridViewTextBoxColumn Column2;
		private DataGridViewTextBoxColumn Column1;
		private DataGridViewTextBoxColumn Column5;
		private DataGridViewTextBoxColumn Column3;
		private DataGridViewTextBoxColumn Column4;
		public Form_Settings()
		{
			this.InitializeComponent();
		}
		private void BindGrid()
		{
			if (Config.RdpInfos != null)
			{
				this.grid.DataSource = Config.RdpInfos.Items;
				this.ListSM.AddRange(Config.RdpInfos.Items);
			}
			else
			{
				RdpInfo item = new RdpInfo
				{
					AliasName = "",
					MachineName = "",
					RdpPort = 3389,
					UserName = "",
					Password = ""
				};
				this.ListSM.Add(item);
			}
			this.LoadStationMonitors();
		}
		private void LoadStationMonitors()
		{
			this.grid.DataSource = null;
			this.grid.DataSource = this.ListSM;
		}
		private bool XmlSave()
		{
			bool result;
			if (this.ListSM.Count == 0)
			{
				MessageBox.Show("请设置远程计算机！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = false;
			}
			else
			{
				foreach (RdpInfo current in this.ListSM)
				{
					if (string.IsNullOrEmpty(current.AliasName))
					{
						MessageBox.Show("别名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						result = false;
						return result;
					}
					if (string.IsNullOrEmpty(current.MachineName))
					{
						MessageBox.Show("计算机不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						result = false;
						return result;
					}
					if (current.RdpPort < 0)
					{
						MessageBox.Show("端口不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						result = false;
						return result;
					}
					if (string.IsNullOrEmpty(current.UserName))
					{
						MessageBox.Show("用户不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						result = false;
						return result;
					}
					if (string.IsNullOrEmpty(current.Password))
					{
						MessageBox.Show("密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						result = false;
						return result;
					}
				}
				RdpInfos rdpInfos = new RdpInfos
				{
					Items = this.ListSM.ToArray()
				};
				Config.RdpInfos = rdpInfos;
				Config.Save();
				Config.Load();
				result = true;
			}
			return result;
		}
		private void Form_Settings_Load(object sender, EventArgs e)
		{
			this.grid.AutoGenerateColumns = false;
			this.BindGrid();
		}
		private void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			if (e.ColumnIndex == 4)
			{
				if (e.Value != null && e.Value.ToString().Length > 0)
				{
					e.Value = new string('*', e.Value.ToString().Length);
				}
			}
		}
		private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			TextBox textBox = e.Control as TextBox;
			if (textBox != null)
			{
				if (this.grid.CurrentCell.ColumnIndex == 4)
				{
					textBox.PasswordChar = '*';
				}
				else
				{
					textBox.PasswordChar = '\0';
				}
			}
		}
		private void grid_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
		}
		private void btnAdd_Click(object sender, EventArgs e)
		{
			RdpInfo item = new RdpInfo
			{
				AliasName = "",
				MachineName = "",
				RdpPort = 3389,
				UserName = "administrator",
				Password = ""
			};
			this.ListSM.Add(item);
			this.LoadStationMonitors();
		}
		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (this.grid.CurrentRow != null)
			{
				this.ListSM.RemoveAt(this.grid.CurrentRow.Index);
				this.LoadStationMonitors();
			}
		}
		private void btnSave_Click(object sender, EventArgs e)
		{
			if (this.XmlSave())
			{
				MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				base.DialogResult = DialogResult.OK;
				base.Close();
			}
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.btnSave = new Button();
			this.btnClose = new Button();
			this.grid = new DataGridView();
			this.btnDelete = new Button();
			this.btnAdd = new Button();
			this.Column2 = new DataGridViewTextBoxColumn();
			this.Column1 = new DataGridViewTextBoxColumn();
			this.Column5 = new DataGridViewTextBoxColumn();
			this.Column3 = new DataGridViewTextBoxColumn();
			this.Column4 = new DataGridViewTextBoxColumn();
			((ISupportInitialize)this.grid).BeginInit();
			base.SuspendLayout();
			this.btnSave.Font = new Font("宋体", 9f);
			this.btnSave.Location = new Point(539, 531);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new Size(75, 24);
			this.btnSave.TabIndex = 5;
			this.btnSave.TabStop = false;
			this.btnSave.Text = "保存";
			this.btnSave.UseVisualStyleBackColor = true;
			this.btnSave.Click += new EventHandler(this.btnSave_Click);
			this.btnClose.Font = new Font("宋体", 9f);
			this.btnClose.Location = new Point(617, 531);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new Size(75, 24);
			this.btnClose.TabIndex = 5;
			this.btnClose.TabStop = false;
			this.btnClose.Text = "关闭";
			this.btnClose.UseVisualStyleBackColor = true;
			this.btnClose.Click += new EventHandler(this.btnClose_Click);
			this.grid.AllowUserToAddRows = false;
			this.grid.AllowUserToResizeRows = false;
			this.grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
			this.grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.grid.Columns.AddRange(new DataGridViewColumn[]
			{
				this.Column2,
				this.Column1,
				this.Column5,
				this.Column3,
				this.Column4
			});
			this.grid.Dock = DockStyle.Top;
			this.grid.Location = new Point(0, 0);
			this.grid.Name = "grid";
			this.grid.RowHeadersVisible = false;
			this.grid.RowHeadersWidth = 20;
			this.grid.RowTemplate.Height = 23;
			this.grid.Size = new Size(704, 525);
			this.grid.TabIndex = 23;
			this.grid.CellFormatting += new DataGridViewCellFormattingEventHandler(this.grid_CellFormatting);
			this.grid.DataError += new DataGridViewDataErrorEventHandler(this.grid_DataError);
			this.grid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(this.grid_EditingControlShowing);
			this.btnDelete.Location = new Point(91, 532);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new Size(75, 23);
			this.btnDelete.TabIndex = 26;
			this.btnDelete.Text = "删除";
			this.btnDelete.UseVisualStyleBackColor = true;
			this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
			this.btnAdd.Location = new Point(12, 532);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new Size(75, 23);
			this.btnAdd.TabIndex = 25;
			this.btnAdd.Text = "添加";
			this.btnAdd.UseVisualStyleBackColor = true;
			this.btnAdd.Click += new EventHandler(this.btnAdd_Click);
			this.Column2.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Column2.DataPropertyName = "AliasName";
			this.Column2.FillWeight = 20f;
			this.Column2.HeaderText = "别名";
			this.Column2.MinimumWidth = 150;
			this.Column2.Name = "Column2";
			this.Column2.Width = 150;
			this.Column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Column1.DataPropertyName = "MachineName";
			this.Column1.FillWeight = 20f;
			this.Column1.HeaderText = "计算机";
			this.Column1.MinimumWidth = 120;
			this.Column1.Name = "Column1";
			this.Column1.Width = 120;
			this.Column5.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Column5.DataPropertyName = "RdpPort";
			this.Column5.FillWeight = 20f;
			this.Column5.HeaderText = "端口";
			this.Column5.MinimumWidth = 60;
			this.Column5.Name = "Column5";
			this.Column5.Width = 60;
			this.Column3.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Column3.DataPropertyName = "UserName";
			this.Column3.FillWeight = 20f;
			this.Column3.HeaderText = "用户名";
			this.Column3.MinimumWidth = 100;
			this.Column3.Name = "Column3";
			this.Column4.DataPropertyName = "Password";
			this.Column4.HeaderText = "密码";
			this.Column4.Name = "Column4";
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(704, 561);
			base.Controls.Add(this.btnDelete);
			base.Controls.Add(this.btnAdd);
			base.Controls.Add(this.grid);
			base.Controls.Add(this.btnClose);
			base.Controls.Add(this.btnSave);
			base.FormBorderStyle = FormBorderStyle.FixedSingle;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "Form_Settings";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = "配置";
			base.Load += new EventHandler(this.Form_Settings_Load);
			((ISupportInitialize)this.grid).EndInit();
			base.ResumeLayout(false);
		}
	}
}
